package com.bolt.common.utils;

import com.bolt.convention.exception.ServiceException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/8/9.
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    public static ApplicationContext applicationContext;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    /**
     * 从spring 上下文中取对象实例
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        try {
            return applicationContext.getBean(name);
        } catch (BeansException ex) {
            return null;
        }
    }

    /**
     * 从spring 上下文中取对像实例
     *
     * @param type
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> type) {
        try {
            return applicationContext.getBean(type);
        } catch (BeansException ex) {
            return null;
        }

    }

    /**
     * 从spring 上下文中取对像实例
     *
     * @param name
     * @param requiredType
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> requiredType) {
        try {
            return SpringContextUtil.applicationContext.getBean(name, requiredType);
        } catch (BeansException ex) {
            return null;
        }
    }

    /**
     * 根据对象全名实例化
     *
     * @param className
     * @return
     */
    public static Object createBean(String className) {
        try {
            Class clazz = com.bolt.common.reflect.ClassLoaderUtil.loadClass(className);
            return createInjectBean(clazz);
        } catch (Exception ex) {
            throw new ServiceException("className new instance error", ex);
        }
    }

    /**
     * 实例化对象并加载依赖
     *
     * @param clazz
     * @return
     */
    public static Object createInjectBean(Class clazz) {
        return SpringContextUtil.applicationContext.getAutowireCapableBeanFactory().createBean(clazz);
    }

    /**
     * 自动加载注解依赖
     *
     * @param o
     */
    public static void inject(Object o) {
        SpringContextUtil.applicationContext.getAutowireCapableBeanFactory().autowireBean(o);
    }


    /**
     * 判断SPRING 容器中是否存在对象实例
     *
     * @param name
     * @return
     */
    public static boolean containsBean(String name) {
        return SpringContextUtil.applicationContext.containsBean(name);
    }

    /**
     * 根据名称判断实例 是否在spring 上下文中单例
     *
     * @param name
     * @return
     */
    public static boolean isSingleton(String name) {
        return SpringContextUtil.applicationContext.isSingleton(name);
    }

    /**
     * 取spring 上下文中对象类型
     *
     * @param name
     * @return
     */
    public static Class<? extends Object> getType(String name) {
        return SpringContextUtil.applicationContext.getType(name);
    }



}
